//
//  HJDataInputStream.m
//  BJH
//
//  Created by xtjun on 2023/10/14.
//

#import "HJDataInputStream.h"



@interface HJDataInputStream()

@property (nonatomic, strong) NSData *data;
@property (nonatomic, assign) NSInteger readLength;


@end


@implementation HJDataInputStream

- (HJDataInputStream *)initWithData:(NSData *)data {
    if (self = [super init]) {
        self.data = [NSData dataWithData:data];
        self.readLength = 0;
    }
    return self;
}

- (NSData *)readDataWithLength:(int)length {
    NSData *data = [self.data subdataWithRange:NSMakeRange(_readLength, length)];
    _readLength += length;
    return data;
}

- (int32_t)readValueWithLength:(int)length {
    int32_t value = [self.data readValueAtRange:NSMakeRange(_readLength, length)];
    _readLength += length;
    return value;
}

- (int8_t)readChar {
    int8_t value = [self.data readValueAtRange:NSMakeRange(_readLength, 1)];
    _readLength += 1;
    return value;
}

- (int16_t)readShort {
    int32_t value = [self.data readValueAtRange:NSMakeRange(_readLength, 2)];
   _readLength += 2;
   return value;
}

- (int32_t)readInt {
    int32_t value = [self.data readValueAtRange:NSMakeRange(_readLength, 4)];
   _readLength += 4;
   return value;
}

- (int64_t)readLong {
    int64_t value = [self.data readValueAtRange:NSMakeRange(_readLength, 8)];
   _readLength += 8;
   return value;
}

- (nullable NSData *)readRemainingData {
    if (self.data.length > _readLength) {
        NSData *data = [self.data subdataWithRange:NSMakeRange(_readLength, self.data.length - _readLength)];
        return data;
    }
    return nil;
}

@end
